package com.rh.number.dto;

import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class BatchQueryDto {
    @Pattern(regexp = "^(|\\d{5,20}(,\\d{5,20})*)$",
            message = "号码必须为5-20位数字，多个用英文逗号分隔")
    private String nums;

    private List<Long> numList;

    public List<Long> getValidatedNumList() {
        if (!StringUtils.hasText(nums)) {
            return Collections.emptyList();
        }

        try {
            numList = Arrays.stream(nums.split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .collect(Collectors.toList());

            if (numList.size() > 5000) {
                throw new IllegalArgumentException("单次最多查询5000个号码");
            }

            return numList;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("包含非数字的号码");
        }
    }

    public String getNums() {
        return nums;
    }
    public void setNums(String nums) {
        this.nums = nums;
    }
}